#include "BulletManager.h"
#include "GameDefine.h"
#include "Bullet.h"
#include "BulletF.h"


BulletManager::BulletManager(void)
{	
	bulletL = new Bullet();
	bulletL->init(B_L);
	
	for (int i = 0; i < 40 ; i++)
	{
		BulletF* b = new BulletF();
		b->init();
		_remainF.push(b);
	}	
	for (int i = 0; i < MAX_BULLET_POOL; i++)
	{
		Bullet* b = new Bullet();	
		_remain.push(b);
	}
}

BulletManager::~BulletManager(void)
{
}

Bullet* BulletManager::getBullet(BULLET_ID id)
{
	Bullet* _rs = NULL;
	switch (id)
	{	
	case B_L:

		return bulletL;
		bulletL->setDamage(4);
		break;	
	case B_F:
		if(_remainF.size() > 0)
		{
			_rs = _remainF.front();
			_remainF.pop();
			_rs->setDamage(1);
		}
		break;
	default:
		if(_remain.size() > 0)
		{
			_rs = _remain.front();
			_rs->init(id);
			_remain.pop();
			_rs->setDamage(10);
		}
		break;
	}
	return _rs;
}

int BulletManager::getRemain(BULLET_ID id)
{
	switch (id)
	{			
	case B_F:
		return _remainF.size();
		break;	
	default:
		return _remain.size();
		break;
	}
	return 0;
}

void BulletManager::addBullet(Bullet* bullet)
{
	_using.push_back(bullet);
}

BulletManager* BulletManager::create()
{
	if(_ins == NULL) _ins = new BulletManager();
	return _ins;
}

BulletManager* BulletManager::getInstance()
{
	return _ins;
}

list<Bullet*> BulletManager::getUsingList()
{
	return _using;
}

void BulletManager::collect(Bullet* bullet)
{
	switch (bullet->getType())
	{	
	case B_F:
		_remainF.push(bullet);
		break;
	default:
		_remain.push(bullet);
		break;
	}
	_using.remove(bullet);
}

void BulletManager::shoot(BULLET_ID id,ObjectType parent, D3DXVECTOR2 start,float angle)
{
	switch (id)
	{	
	case B_S:
		{
			int size = _remain.size();
			if(size < 3) break;
			if(size < 5)
			{
				float dx =   -  DAS;
				for (int i = 0 ; i < 3 ; i++)
				{
					Bullet* b = getBullet(id);
					b->loaded(parent,start,angle + dx);
					addBullet(b);
					dx += DAS;
				}			
			}
			else
			{
				float dx =   - 2 * DAS;
				for (int i = 0 ; i < 5 ; i++)
				{
					Bullet* b = getBullet(id);
					b->loaded(parent,start,angle + dx);
					addBullet(b);
					dx += DAS;
				}
			}			
		}
		break;
	case B_L:
		_using.remove(bulletL);
		bulletL->loaded(parent,start,angle);
		addBullet(bulletL);
		break;
	default:
		Bullet* i = getBullet(id);
		if(i == NULL) return;
		i->loaded(parent,start,angle);
		addBullet(i);
		break;
	}	
}


